package cn.cycleif.app.controller;

import cn.cycleif.app.domain.dto.UserChangeMailDTO;
import cn.cycleif.app.domain.dto.UserChangePwdDTO;
import cn.cycleif.app.domain.dto.UserProfileDTO;
import cn.cycleif.app.domain.dto.UserRegisterDTO;
import cn.cycleif.app.service.UserService;
import cn.cycleif.util.R;
import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.dev33.satoken.annotation.SaIgnore;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @Date: 25/1/22-22:48
 * @Desc:
 */
@RestController
@RequestMapping("/api/user")
@Tag(name = "用户管理", description = "提供注册，改密，获取信息等")
@SaIgnore
public class UserController {

  @Autowired
  private UserService userService;

  @PostMapping("/register")
  @Operation(summary = "用户注册")
  public R<String> register(@RequestBody @Valid UserRegisterDTO userRegisterDTO) {
    return userService.insert(userRegisterDTO);
  }

  @GetMapping("/{id}")
  @Operation(summary = "根据id查询用户信息")
  public R<Object> getUserInfoById(@PathVariable("id") Integer id) {
    return userService.getUserInfoById(id);
  }

  @GetMapping("/name/{id}")
  @Operation(summary = "根据用户名查询用户信息")
  public R<Object> getUserInfoByName(@PathVariable("id") String name) {
    return userService.getUserInfoByName(name);
  }

  @PostMapping("/changeMail")
  @Operation(summary = "修改邮箱")
  @SaCheckLogin
  public R<String> changeMail(@RequestBody @Valid UserChangeMailDTO user) {
    return userService.changeMail(user);
  }

  @PostMapping("/changePwd")
  @Operation(summary = "修改密码")
  @SaCheckLogin
  public R<String> changePwd(@RequestBody @Valid UserChangePwdDTO user) {
    return userService.changePwd(user);
  }

  @GetMapping()
  @Operation(summary = "获取当前用户信息")
  @SaCheckLogin
  public R<Object> getCurrentUserInfo() {
    return userService.getCurrentUserInfo();
  }

  @PutMapping
  @Operation(summary = "更新用户")
  @SaCheckLogin
  public R<String> update(@RequestBody UserProfileDTO userProfileDTO) {
    return userService.updateUser(userProfileDTO);
  }

  @GetMapping("/rank")
  @Operation(summary = "获取用户排名")
  public R<Object> getRankList() {
    return userService.getRankList();
  }

  @GetMapping("/staff")
  @Operation(summary = "获取员工列表")
  public R<Object> getStaffList() {
    return userService.getStaffList();
  }
}
